<template>
  <div class="mod-menu">

    <el-form :inline="true" :model="dataForm">
      <el-form-item>
        <!-- <el-button @click="getList()">查询</el-button> -->
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" row-key="Id" border style="width: 100%">

      <el-table-column prop="course_name" align="center" label="分类名称">
      </el-table-column>
      <el-table-column prop="article_num" align="center" label="文章数量">
      </el-table-column>

      <el-table-column prop="order_index" align="center" label="分类排序">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row)">修改</el-button>
          <el-button type="text" class="c-error" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="params.page" :page-sizes="[10, 20, 50, 100]" :page-size="params.limit" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <classify-add-update ref="classifyAddUpdate" @getList="getList"></classify-add-update>
  </div>
</template>

<script>
import classifyAddUpdate from './classify-add-update'
import serviceApi from '@/api/serviceApi.js'
export default {
  data () {
    return {
      dataForm: {},
      dataList: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
      params: {
        page: 1,
        limit: 10
      },
      totalPage: 0
    }
  },
  components: {
    classifyAddUpdate
    // ApplyAudit
  },
  mounted () {
    this.getList()
  },
  methods: {
    // 每页数
    sizeChange (val) {
      this.params.limit = val
      this.getList()
    },
    // 当前页
    currentChange (val) {
      this.params.page = val
      this.getList()
    },
    getList () {
      serviceApi.classifyList(this.params).then((res) => {
        if (res && res.data.code == 200) {
          this.dataList = res.data.page.list
          this.totalPage = res.data.page.totalCount
        }
      })
    },

    // 新增 / 修改
    addOrUpdateHandle (row) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.classifyAddUpdate.init(row)
      })
    }, // 删除
    deleteHandle (id) {
      this.$confirm(`确定要删除该条数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          serviceApi.classifyDelete(id).then((res) => {
            if (res && res.data.code == 200) {
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.getList()
            }
          })
        })
        .catch(() => { })
    }
  }
}
</script>
